﻿using Mozhi.Abp.Core.Extensions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp;
using Volo.Abp.Domain.Values;

namespace Mozhi.Abp.Domain.BasicData
{
    public class Organization : ValueObject
    {
        [JsonProperty]
        public Guid Id { get; private set; }

        [JsonProperty]
        public string Name { get; private set; }

        [JsonProperty]
        public string OfficialName { get; private set; }

        [JsonProperty]
        public DateTime? ActivatedDate { get; private set; }

        [JsonProperty]
        public int? AccountingStandard { get; private set; }

        [JsonProperty]
        public Guid? StandardCurrencyId { get; private set; }

        [JsonProperty]
        public string TaxIDNumber { get; private set; }

        [JsonProperty]
        public bool Initialized {  get; private set; }

        private Organization() { }

        public void ValidateDocumentDate(DateTime date)
        {
            if (!ActivatedDate.HasValue)
                throw new UserFriendlyException("组织未设置启用日期", "100000");

            if (!Initialized && date.ToPeriod() >= ActivatedDate.Value.ToPeriod())
                throw new UserFriendlyException("组织还未完成业务初始化，无法录入启用日期之后的单据", "100000");

            if (Initialized && date.ToPeriod() < ActivatedDate.Value.ToPeriod())
                throw new UserFriendlyException("单据日期不能小于组织启用日期", "100000");
        }

        public void ValidateFinance()
        {
            if (!ActivatedDate.HasValue)
                throw new UserFriendlyException("组织未设置启用日期", "100000");

            if (!StandardCurrencyId.HasValue)
                throw new UserFriendlyException("组织未设置本位币", "100000");
        }

        public void ValidateFinanceDate(DateTime date)
        {
            ValidateFinance();

            var openingPeriod = ActivatedDate?.ToPeriod();

            if (date < openingPeriod.Value.ToFirstDayDateTime())
                throw new UserFriendlyException("日期不能小于账套启用日期", "100000");

        }

        protected override IEnumerable<object> GetAtomicValues()
        {
            throw new NotImplementedException();
        }
    }
}
